package com.tky.conference.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.tky.conference.R;
import com.tky.conference.base.BaseActivity;
import com.tky.conference.base.BaseApplication;
import com.tky.conference.dao.LoginInfo;
import com.tky.conference.entity.FailRequest;
import com.tky.conference.implInterfaces.LoginInfoImpl;
import com.tky.conference.interfaces.HttpResultListener;
import com.tky.conference.utlils.LoginSP;
import com.tky.conference.utlils.ShowDialog;
import com.tky.conference.utlils.ToastUtil;
import com.tky.conference.utlils.UIUtils;

import java.util.Map;
import java.util.Set;

/**
 * Created by yy on 2015/12/3.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener,HttpResultListener{
    private EditText mUserName;
    private EditText mPassword;
    private Button mLogin;
    private CheckBox mIsRemember;
    private boolean isSaveLoginInfo=false;
    private LoginSP mLoginSP;
    private Toolbar toolbar;
    private ShowDialog showDialog;
    @Override
    public void initView() {
        setContentView(R.layout.activity_login);
        mUserName= (EditText) findViewById(R.id.username);
        mPassword= (EditText) findViewById(R.id.password);
        mLogin= (Button) findViewById(R.id.login);
        mIsRemember= (CheckBox) findViewById(R.id.isRemember);
    }

    @Override
    public void initDates() {
        mLoginSP= LoginSP.getInstance(BaseApplication.getContext());
        showDialog=new ShowDialog(LoginActivity.this);
    }

    @Override
    public void initToolBar() {

    }

    @Override
    public void initEvent() {
        mLogin.setOnClickListener(this);
        mIsRemember.setOnCheckedChangeListener(this);
        if (mLoginSP.isLoginInfoExist()){
            LoginSP.SpLoginIdentity loginIdentity=mLoginSP.getLoginIdentity();
            mUserName.setText(loginIdentity.getLoginName());
            mPassword.setText(loginIdentity.getPwd());
            mIsRemember.setChecked(true);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        this.isSaveLoginInfo=isChecked;
    }

    @Override
    public void onClick(View v) {
        final String username=mUserName.getText().toString().trim();
        final String password=mPassword.getText().toString().trim();
        if (TextUtils.isEmpty(username)){
            ToastUtil.showToast(this, "用户名不能为空");
            return;
        }
        if (TextUtils.isEmpty(password)){
            ToastUtil.showToast(this, "密码不能为空");
            return;
        }
        if (isSaveLoginInfo){
            mLoginSP.saveLoginInfo(username,password);
        }else {
            mLoginSP.clearLoginInfo();
        }
        //请求网络
        LoginInfoImpl loginInfoImpl=new LoginInfoImpl(LoginActivity.this);
        loginInfoImpl.getLoginInfo(username, password);

    }

    private void requetPost(String username, String password) {

    }

    @Override
    public void onStartRequest() {
        showDialog.Loading("正在登录...");
    }

    @Override
    public void onResult(Object obj) {
        if (obj instanceof FailRequest){
            if (!((FailRequest) obj).getStatus().equals(0)){
                showDialog.dismissDialog();
                ToastUtil.showToast(this, ((FailRequest) obj).getMsg());
            }
        }else if (obj instanceof LoginInfo){
            showDialog.dismissDialog();
            Intent intent=new Intent(BaseApplication.getContext(),MainActivity.class);
            SharedPreferences sharedPreferences=getSharedPreferences("save_account", MODE_PRIVATE);
            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putString("account",((LoginInfo) obj).getUsername());
            editor.commit();
            UIUtils.startActivity(intent);
        }
    }

    @Override
    public void onFinish() {

    }
}
